package com.example.controller.webchat;

import com.example.dto.CartDTO;
import com.example.excel.CartExcel;
import com.example.service.CartService;

import com.example.model.api.Result;
import com.example.model.page.PageData;
import com.example.validator.AssertUtils;
import com.example.validator.ValidatorUtils;
import com.example.validator.group.AddGroup;
import com.example.validator.group.DefaultGroup;
import com.example.validator.group.UpdateGroup;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jakarta.servlet.http.HttpServletResponse;
import com.example.utils.ExcelUtils;

/**
* 购物车表
*
* @author yyyyjinying 1020066395@qq.com
* @since 1.0-SNAPSHOT 2025-05-22
*/
@RestController
@RequestMapping("webchat/cart")
@Tag(name = "购物车表")
public class CartController {
    @Autowired
    private CartService cartService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @RequestMapping(value = "page", method = {RequestMethod.POST, RequestMethod.GET})
//    @PreAuthorize("hasAuthority('sys:cart:page')")
    public Result<PageData<CartDTO>> page(@RequestParam Map<String, Object> params){
        PageData<CartDTO> page = cartService.page(params);

        return new Result<PageData<CartDTO>>().ok(page);
    }

    @GetMapping("{id}")
    @Operation(summary = "信息")
//    @PreAuthorize("hasAuthority('sys:cart:info')")
    public Result<CartDTO> get(@PathVariable("id") Long id){
        CartDTO data = cartService.get(id);

        return new Result<CartDTO>().ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
//    @PreAuthorize("hasAuthority('sys:cart:save')")
    public Result<String> save(@RequestBody CartDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, AddGroup.class, DefaultGroup.class);

        cartService.save(dto);

        return new Result<String>().ok("ok");
    }

    @PutMapping
    @Operation(summary = "修改")
//    @PreAuthorize("hasAuthority('sys:cart:update')")
    public Result<String> update(@RequestBody CartDTO dto){
        //效验数据
        ValidatorUtils.validateEntity(dto, UpdateGroup.class, DefaultGroup.class);

        cartService.update(dto);

        return new Result<String>().ok("ok");
    }

    @DeleteMapping
    @Operation(summary = "删除")
//    @PreAuthorize("hasAuthority('sys:cart:delete')")
    public Result<String> delete(@RequestBody Long[] ids){
        //效验数据
        AssertUtils.isArrayEmpty(ids, "id");

        cartService.delete(ids);

        return new Result<String>().ok("ok");
    }

    @GetMapping("export")
    @Operation(summary = "导出")
//    @PreAuthorize("hasAuthority('sys:cart:export')")
    public void export(@RequestParam Map<String, Object> params, HttpServletResponse response) throws Exception {
        List<CartDTO> list = cartService.list(params);

        ExcelUtils.exportExcelToTarget(response, null, "购物车表", list, CartExcel.class);
    }

}